package chat7me_1223.client.ui;

import chat7me_1223.client.Client;
import chat7me_1223.client.util.IOUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.OutputStream;

public class TalkWindow {
    private JFrame win=null;
    private JPanel contentPane=null;
    private JPanel jPanel=null;
    private JScrollPane jScrollPane=null;
    private JTextArea jTextAreaMessages=null;
    private JTextField msgInput=null;
    private JButton sendBtn=null;
    private Client client;
    private String login_username;
    private String sendTo_Username;
    private OutputStream out;

    public TalkWindow(String fromUser,String targetName,Client c){
        //this.login_username=fromUser;
        this.sendTo_Username=targetName;
        this.client=c;

        try {
            out=client.getSocket().getOutputStream();
        }catch (IOException e){
            e.printStackTrace();
        }

        win=new JFrame(client.getLoginUsername()+"群聊中..");
        win.setSize(361,349);
        win.setResizable(false);

        contentPane=new JPanel();
        contentPane.setLayout(null);

        jPanel=new JPanel();
        jPanel.setLayout(null);
        jPanel.setBounds(new Rectangle(0, 1, 345, 310));

        jScrollPane=new JScrollPane();
        jScrollPane.setBounds(new Rectangle(1, 1, 336, 194));

        jTextAreaMessages=new JTextArea();
        jTextAreaMessages.setEnabled(true);
        jTextAreaMessages.setEditable(false);

        jScrollPane.setViewportView(jTextAreaMessages);

        jPanel.add(jScrollPane);

        msgInput=new JTextField();
        msgInput.setBounds(new Rectangle(5, 203, 333, 35));
        msgInput.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==KeyEvent.VK_ENTER){
                    sendMessageToServer();
                }
            }
        });
        jPanel.add(msgInput);



        sendBtn=new JButton("发送");
        sendBtn.setBounds(new Rectangle(193, 254, 138, 49));
        sendBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sendMessageToServer();
            }
        });

        jPanel.add(sendBtn);
        contentPane.add(jPanel);
        win.setContentPane(contentPane);
        win.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                client.removeTalkWindowInClient(TalkWindow.this);
                win.dispose();
            }
        });
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        win.setLocation(420,200);
        win.setVisible(true);

    }


    public void showAllMessage(String fromUserStr, String msg){
        this.jTextAreaMessages.setText(this.jTextAreaMessages.getText()+fromUserStr+"说：\n"+msg+"\n");
        this.jScrollPane.getVerticalScrollBar().setValue(jScrollPane.getVerticalScrollBar().getMaximum());
    }

    public boolean sendMessageToServer(){
        if(client.getOnLineUserList().size()==0){
            jTextAreaMessages.setText("已无用户在线...\n");
            this.jScrollPane.getVerticalScrollBar().setValue(jScrollPane.getVerticalScrollBar().getMaximum());
            return false;
        }

        if("ALL".equals(sendTo_Username)){
            IOUtil.writeShort(Client.SEND_TEXT_REQUEST, out);
            IOUtil.writeString(msgInput.getText(), out);
            jTextAreaMessages.setText(jTextAreaMessages.getText()+ "我说:\n   "+ msgInput.getText() + "\n");
            jScrollPane.getVerticalScrollBar().setValue(jScrollPane.getVerticalScrollBar().getMaximum());
            msgInput.setText("");
            return  true;
        }
        return  false;
    }


}
